using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System.Data.Common;

using NBear.Common;
using NBear.Data;

namespace NBear.Test.UnitTests
{
    /// <summary>
    /// Summary description for TestBatchDataAccess
    /// </summary>
    [TestClass]
    public class TestBatchDataAccess
    {
        private Gateway gateway;

        public TestBatchDataAccess()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            gateway = new Gateway(DatabaseType.SqlServer, "Server=(local);Database=Northwind;Uid=sa;Pwd=sa;Pooling=false");
            //Gateway.SetSqlLogger(new LogHandler(Console.Write));
        }
        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            gateway.Delete<Category>("CategoryName = @Name", new object[] { "test" });
        }
        //
        #endregion

        private int times = 500;

        [TestMethod]
        public void TestNormalWrite()
        {
            long tick = DateTime.Now.Ticks;
            NormalWrite();
            tick = DateTime.Now.Ticks - tick;
            Console.WriteLine(string.Format("Normal Write: {0}", tick));
        }

        [TestMethod]
        public void TestBatchWrite()
        {
            long tick = DateTime.Now.Ticks;
            BatchWrite();
            tick = DateTime.Now.Ticks - tick;

            Console.WriteLine(string.Format("Batch Write: {0}", tick));
        }

        private object NormalWrite()
        {
            Category obj = EntityFactory<Category>.CreateObject();
            obj.CategoryName = "test";
            obj.Description = "test";
            obj.Picture = new byte[1];

            for (int i = 0; i < times; i++)
            {
                gateway.Insert<Category>(obj);
            }

            return null;
        }

        private object BatchWrite()
        {
            Category obj = EntityFactory<Category>.CreateObject();
            obj.CategoryName = "test";
            obj.Description = "test";
            obj.Picture = new byte[1];

            Gateway batch = gateway.BeginBatchGateway(10);
            for (int i = 0; i < times; i++)
            {
                batch.Insert<Category>(obj);
            }
            batch.EndBatch();

            return null;
        }

    }
}
